package yw

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func material(r *gin.RouterGroup) {
	material := r.Group("/material")
	{
		material.POST(sql.RequestAdd, materialAdd)
		material.POST(sql.RequestUpdate, materialUpdate)
		material.GET(sql.RequestDelete, materialDelete)
		material.GET(sql.RequestList, materialList)
		material.GET(sql.RequestData, materialData)
	}
}

func materialAdd(c *gin.Context) {
	sql.HttpAddTable(c, "yw_material", &model.Material{}, nil)
}

func materialUpdate(c *gin.Context) {
	sql.HttpUpdatesTable(c, "yw_material", &model.Material{}, func(db *gorm.DB) *gorm.DB {

		return db
	})
}

func materialDelete(c *gin.Context) {
	sql.HttpDelete(c, &model.Material{}, func(db *gorm.DB) *gorm.DB {
		id := c.Query("id")
		var materialBean model.Material
		db.Where("id = ?", id).First(&materialBean)
		if materialBean.InventoryWeight != materialBean.MaterialWeight {

		}

		if id == "" {
			db.Error = errors.New(sql.ErrorParams)
		} else {
			db = db.Table("yw_material").Where("id = ?", id)
		}
		return db

	})
}

func materialList(c *gin.Context) {
	sql.HttpList(c, &[]model.Material{}, func(db *gorm.DB) *gorm.DB {
		db = db.Table("yw_material").Order("date DESC")
		return db
	})
}

func materialData(c *gin.Context) {
	sql.HttpData(c, &[]model.Material{}, func(db *gorm.DB) *gorm.DB {
		db = db.Table("yw_material")
		return db
	})
}
